//
//  LeftViewController.m
//  EarnCommision
//
//  Created by EvanZ on 15/3/12.
//  Copyright (c) 2015年 EC. All rights reserved.
//

#import "LeftViewController.h"
#import "HomeViewController.h"
#import "PersonalCenterTableViewController.h"
#import "HelpCenterViewController.h"
#import "FeedBackViewController.h"
#import "MarkUsViewController.h"
#import "AboutUsViewController.h"
#import "IIViewDeckController.h"
#import "UIColor+CreateMethod.h"

@interface LeftViewController (){
    UITableView * m_tableView;
    NSMutableArray * m_itemArray;
    NSMutableArray * m_cellArray;
}

@end

@implementation LeftViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    NSDictionary * dic1 = [[NSDictionary alloc] initWithObjectsAndKeys:@"首页",@"title",[HomeViewController class],@"class", nil];
    NSDictionary * dic2 = [[NSDictionary alloc] initWithObjectsAndKeys:@"个人中心",@"title",[PersonalCenterTableViewController class],@"class", nil];
    NSDictionary * dic3 = [[NSDictionary alloc] initWithObjectsAndKeys:@"帮助中心",@"title",[HelpCenterViewController class],@"class", nil];
    NSDictionary * dic4 = [[NSDictionary alloc] initWithObjectsAndKeys:@"用户反馈",@"title",[FeedBackViewController class],@"class", nil];
    NSDictionary * dic5 = [[NSDictionary alloc] initWithObjectsAndKeys:@"给我打分",@"title",[MarkUsViewController class],@"class", nil];
    NSDictionary * dic6 = [[NSDictionary alloc] initWithObjectsAndKeys:@"关于我们",@"title",[AboutUsViewController class],@"class", nil];


    m_itemArray = [[NSMutableArray alloc]initWithObjects:dic1,dic2,dic3,dic4,dic5,dic6, nil];
    
    m_tableView = [[UITableView alloc]initWithFrame:self.view.frame];
    m_tableView.delegate = self;
    m_tableView.dataSource = self;
    [self.view addSubview:m_tableView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [m_itemArray count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    LeftMenuCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[LeftMenuCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    if (indexPath.row == 0) {
        [cell setSelect:YES];
    }
    [m_cellArray addObject:cell];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    [cell setTitle:[NSString stringWithFormat:@"%@",[[m_itemArray objectAtIndex:indexPath.row]valueForKey:@"title"]]];
    return cell;
}


#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row < [m_itemArray count]) {
        LeftMenuCell * cell = (LeftMenuCell*)[tableView cellForRowAtIndexPath:indexPath];
        
        //设置选中状态
        for (LeftMenuCell * cell in m_cellArray) {
            [cell setSelect:NO];
        }
        [cell setSelect:YES];
        
        Class className = [[m_itemArray objectAtIndex:indexPath.row] valueForKey:@"class"];
        if (className) {
            [self.viewDeckController closeLeftViewBouncing:^(IIViewDeckController * viewDeckController){
                 UIViewController * vc = [[className alloc] initWithNibName:nil bundle:nil];
                 UINavigationController * navController = [[UINavigationController alloc]initWithRootViewController:vc];
                 self.viewDeckController.centerController = navController;
                 self.viewDeckController.rightController = nil;
             }];
        }
    }
}


@end

@implementation LeftMenuCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(50, 10, 300, 24)];
        [titleLabel setBackgroundColor:[UIColor clearColor]];
        [titleLabel setFont:[UIFont fontWithName:@"Helvetica" size:16]];
        [titleLabel setTextColor:[UIColor darkGrayColor]];
        [self addSubview:titleLabel];
        
        self.selectionStyle = UITableViewCellSelectionStyleGray;
    }
    return self;
}

- (void)setTitle:(NSString *)title
{
    [titleLabel setText:title];
}

- (void)setSelect:(BOOL)select
{
    if (select) {
    }
    else
    {
    }
}
@end